from flask import Flask, jsonify, send_from_directory, Response, request
import os
from flask_cors import CORS #pip install flask-cors


app = Flask(__name__)
CORS(app)

current_dir = os.path.dirname(os.path.abspath(__file__))
VTK_DIR = f"{current_dir}/vtk-files"  # VTK文件存储目录
print(f"VTK_DIR: {VTK_DIR}");

# 1. 获取文件列表接口
@app.route('/api/vtk-files')
def list_vtk_files():
    files = [f for f in os.listdir(VTK_DIR) if f.endswith('.vtk')]
    return jsonify(files)

# 2. 文件访问接口
@app.route('/api/vtk-file/download/<filename>')
def download_vtk_file(filename):
    return send_from_directory(VTK_DIR, filename)

# 2. 文件访问接口
@app.route('/api/vtk-file/content/<filename>')
def content_vtk_file(filename):
    file_path = os.path.join(VTK_DIR, filename)
    if os.path.exists(file_path) and os.path.isfile(file_path):
        with open(file_path, 'r') as f:
            content = f.read()
        return Response(content, mimetype='text/plain')
    else:
        return jsonify({'error': 'File not found'}), 404

if __name__ == '__main__':
    app.run(port=5000)